<template>
  <div class="fileManage">
    <basic-container>
      <avue-crud
        :option="fileListOption"
        :data="data"
        @current-change="currentChange"
        @size-change="sizeChange"
        @selection-change="(v) => (selection = v)"
        ref="crud"
        :page.sync="page"
      >
        <template slot="menuLeft">
          <el-button size="small" type="primary" @click="addFile"
            >新增
          </el-button>
          <el-button
            size="small"
            :disabled="selection.length === 0"
            type="primary"
            @click="delFile"
            >删除
          </el-button>
        </template>
        <template slot="menu" slot-scope="{ row }">
          <el-button type="text" @click="downLoadFile(row)"> 下载 </el-button>
          <el-button type="text" @click="deleteRow(row)"> 删除 </el-button>
        </template>
      </avue-crud>
     
    </basic-container>
  </div>
</template>
 
<script>
import commonJs from "@/util/commonJs";
import { urlDownload } from "@/util/util";
import * as api from "@/api/fileList/index";
import {EventBus} from "@/components/javaScript/EventBus";

export default {
  name: "fileList",
  mixins: [commonJs],
  inject: ["fileListOption"],
  data() {
    return {
      selection: [],
    };
  },
  methods: {
    
    delData(data) {
      this.$confirm("确定要删除此项吗？", "警告", {
        type: "warning",
      })
        .then(() => {
          api.del({ ids: data }).then((res) => {
            this.$message.success("删除成功！");
            this.getList();
          });
        })
        .catch(() => {});
    },
    delFile() {
      if (this.selection.length === 0) {
        this.$message.error("请先选择！");
        return;
      }
      const listData = this.selection.map((row) => row.oid);
      this.delData(listData.join(","));
    },
    deleteRow(row) {
      if (!row.oid) {
        return;
      }
      this.delData(row.oid);
    },

    downLoadFile(data) {
      api.downloadStorageFile({ identifier: data.fileIdentifier }).then((res) => {
        if (res.data.success) {
          urlDownload(res.data.data, data.fileName);
        } else {
          this.$message.error("下载异常！");
        }
      });
    },
    addFile() {
      EventBus.$emit('upload')
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList();
    },
  },
  mounted(){
    EventBus.$on("refreshFile",()=>{
      this.getList();
    });

  },
    beforeDestroy() {
    EventBus.$off("refreshFile");
  },
};
</script>